package top.zhangjianyong.entity;


import top.zhangjianyong.utils.PhotoUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2022/6/24 10:32 AM
 */
public class Photo {
    private final static DateTimeFormatter DTF = DateTimeFormatter.ofPattern("HH:mm:ss");
    private BufferedImage img; //图片数据
    private LocalTime time; //图片时间


    private String videoName;
    private List<BufferedImage> imgs;
    private List<int[]> datas;
    private int[] data;
    private int rows;
    private int cols;

    public Photo(byte[] content, long timestamp, int rows, int cols, String fileName) throws IOException {
        rows = rows == 0 ? 35 : rows;
        cols = cols == 0 ? 64 : cols;
        this.img = ImageIO.read(new ByteArrayInputStream(content));
        this.time = LocalTime.ofNanoOfDay(timestamp);
        this.rows = rows;
        this.cols = cols;
        //切分子图片
        this.imgs = PhotoUtil.cutImage(this.img, rows, cols);
        //计算子图直方图
        this.datas = this.imgs.stream().map(PhotoUtil::getData).collect(Collectors.toList());
        //this.data = PhotoUtil.getData(this.img);
        this.videoName = fileName;
    }

    public int[] getData() {
        return data;
    }

    public void setData(int[] data) {
        this.data = data;
    }

    public String getVideoName() {
        return videoName;
    }

    public void setVideoName(String videoName) {
        this.videoName = videoName;
    }

    public List<BufferedImage> getImgs() {
        return imgs;
    }

    public void setImgs(List<BufferedImage> imgs) {
        this.imgs = imgs;
    }

    public List<int[]> getDatas() {
        return datas;
    }

    public void setDatas(List<int[]> datas) {
        this.datas = datas;
    }

    public BufferedImage getImg() {
        return img;
    }

    public void setImg(BufferedImage img) {
        this.img = img;
    }

    public LocalTime getTime() {
        return time;
    }

    public void setTime(LocalTime time) {
        this.time = time;
    }


    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getCols() {
        return cols;
    }

    public void setCols(int cols) {
        this.cols = cols;
    }

    public String getFileName(String pre) {
        int random = (int) (Math.random() * 100000);
        if (pre == null || pre.length() == 0) return videoName + time.format(DTF) + "_" + random + ".jpg";
        return pre + videoName + "-" + time.format(DTF) + "_" + random + ".jpg";
    }
}
